package com.bhkj.pdjhforotherapp;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.bhkj.pdjhforotherapp.core.printutilspdj.UsbPrintManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.reader.sdk.CardReader;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//    private IDCardReaderClient builder;
    private UsbPrintManager usbPrintManager;
    private CardReader instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_test);
        findViewById(R.id.read).setOnClickListener(this);
        findViewById(R.id.tv_test_click).setOnClickListener(this);
        findViewById(R.id.close).setOnClickListener(this);
        instance = CardReader.getInstance(getApplication());
//        initDevice();
    }


    private void initPrint(){
        if (usbPrintManager==null){
            usbPrintManager = new UsbPrintManager();
        }
    }


    private void initDevice(){
//        builder = IDCardReaderClient.build()
//                .setContext(this)
//                .setDeviceType(Contacts.USB_MODE)
//                .setReaderResultCallback(res -> runOnUiThread(() -> {
//                    if (res.contains("打开端口失败")){
//                        initDevice();
//                    }
//                    ToastUtils.showShort(res);
//                })).builder();
    }

    @Override
    public void onClick(View view) {

        final int viewId = view.getId();
        if (viewId == R.id.close) {
//            if (usbPrintManager!=null){
//                usbPrintManager.closePrint();
//            }
//            builder.closeConnect();
            instance.close();
        } else if (viewId==R.id.read){
            instance.readId(readerBean -> runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showShort(readerBean.getName()+"-"+readerBean.getIdNum());
                }
            }));
        }else {
            instance.init(readerBean -> LogUtils.d(readerBean.getMessage()));

//            builder.readIDCard();
//            initPrint();
//            usbPrintManager.showPrintDeviceStatus();
//            Bills.printSmallTicket(usbPrintManager.getUsbDriver(),"测试","测试","测试","测试","测试",0);

        }
    }


}
